home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 May: Tool Chest / Developer CD Series May 1996 (Tool Chest) (Apple Computer) (1996).iso / Sample Code / Snippets / QuickDraw / MakeITable / MakeITable.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-02-07  |  12.0 KB  |  486 lines  |  [TEXT/MPS ]

  1. /*______________________________________________________*/
  2. /*                            C Shell                        */
  3. /*                          by                          */
  4. /*                  RICHARD P. COLLYER                  */
  5. /*                       09/02/87                       */
  6. /*______________________________________________________*/
  7.  
  8. #include    <CType.h>
  9. #include    <Quickdraw.h>
  10. #include    <QDOffscreen.h>
  11. #include    <Windows.h>
  12. #include    <OSUtils.h>
  13. #include    <Controls.h>
  14. #include    <desk.h>
  15. #include    <dialogs.h>
  16. #include    <Events.h>
  17. #include    <Memory.h>
  18. #include    <Menus.h>
  19. #include    <Notification.h>
  20. #include    <OSEvents.h>
  21. #include    <Packages.h>
  22. #include    <Palettes.h>
  23. #include    <Printing.h>
  24. #include    <Resources.h>
  25. #include    <SANE.h>
  26. #include    <SegLoad.h>
  27. #include    <Sound.h>
  28. #include    <Slots.h>
  29. #include    <ToolUtils.h>
  30. #include    <StdIO.h>
  31. #include    <math.h>
  32.  
  33. extern _DataInit();
  34.  
  35. #define    TRUE            0xFF
  36. #define    FALSE            0
  37. #define    VERSION            1
  38. #define    SR_BIT            0
  39.  
  40. #define    appleID            128            
  41. #define    appleMenu        0
  42. #define    aboutMeCommand    1
  43.  
  44. #define    fileID            129
  45. #define    startCommand    1
  46. #define    printCommand    3
  47. #define    quitCommand     5
  48.  
  49. #define    editID            130
  50.  
  51. #define    aboutMeDLOG        128
  52. #define    okButton        1
  53. #define    authorItem        2
  54. #define    languageItem    3
  55.  
  56. long                Tick, WhereBoard;
  57. int                    yieldTime, err, numcolor, offLeft, offTop, offRight, offBottom;
  58. Rect                screenRect, BaseRect, TotalRect, minRect;
  59. WindowPtr            whichWindow, myWindow;
  60. CTabHandle            mycolors;
  61. PaletteHandle        srcPalette;
  62. MenuHandle            mymenu1, mymenu2, mymenu0;
  63. EventRecord         myEvent;
  64. Boolean                DoneFlag, Color, CPU20, FPU, Sys6, Memboard;
  65. GDHandle            theGDevice;
  66. SysEnvRec            theWorld;
  67. OSErr                OSys;
  68. SpBlockPtr            mySpBlockPtr;
  69. SpBlock                mySpBlock;
  70.  
  71.  
  72. #ifdef powerc
  73.    QDGlobals    qd;
  74. #endif
  75.  
  76. /*______________________________________________________*/
  77. /*                What ever You want                    */
  78. /*______________________________________________________*/
  79. void start()
  80. {
  81.     GDHandle    theGDHand;
  82.     
  83.     theGDHand = GetDeviceList();
  84.     do {
  85.         HLock ((Handle) theGDHand);
  86.         if (TestDeviceAttribute (theGDHand, 13) && 
  87.                 TestDeviceAttribute (theGDHand, 15) &&
  88.                 (*theGDHand)->gdType == 0) {
  89.             (*theGDHand)->gdResPref = 5;
  90.             (*(*theGDHand)->gdITable)->iTabSeed = GetCTSeed();
  91.             GDeviceChanged (theGDHand);
  92.             HUnlock ((Handle) theGDHand);
  93.             }
  94.         } while ((theGDHand = GetNextDevice(theGDHand)) != nil);
  95.  
  96.     return;
  97. }
  98.  
  99. /*______________________________________________________*/
  100. /*                     Draw Stuff                       */
  101. /*______________________________________________________*/
  102. void DrawStuff (theRect)
  103.  
  104.     Rect         theRect;
  105. {
  106.     SetRect (&theRect, 10, 10, 200, 200);
  107.     
  108.     CopyBits (&qd.screenBits, &qd.thePort->portBits, &qd.screenBits.bounds, 
  109.                 &qd.screenBits.bounds, srcCopy, nil);
  110.  
  111.  
  112. /**
  113.     CopyBits (&window->portBits, &window->portBits, &window->portRect, &window->portRect, srcOr, nil);
  114.     CopyBits (&windpeek->port.portBits, &windpeek->port.portBits, &window->portRect, &window->portRect, srcOr, nil);
  115.     CopyBits (&screenBits, &(*thePort).portBits, &theWorld, &theWorld, srcOr, nil);
  116. */
  117.     return;
  118. }  /* DrawStuff */
  119.  
  120. /*______________________________________________________*/
  121. /*                        Print                         */
  122. /*______________________________________________________*/
  123. void Print()
  124. {
  125.     TPPrPort    thePrPort;
  126.     TPrStatus    theStatus;
  127.     GrafPtr        oldPort;
  128.     THPrint        thePrRecHdl;
  129.     
  130.     GetPort(&oldPort);
  131.     
  132.     thePrRecHdl = (THPrint)  NewHandle (sizeof (TPrint));
  133.     
  134.     PrOpen();
  135.  
  136.         if (PrError() == noErr)
  137.          {
  138.             PrintDefault(thePrRecHdl);
  139.             
  140.             if (! PrStlDialog(thePrRecHdl)) 
  141.               ExitToShell();
  142.               
  143.             if (! PrJobDialog(thePrRecHdl)) 
  144.               ExitToShell();
  145.               
  146.             thePrPort = PrOpenDoc(thePrRecHdl, nil, nil);
  147.         
  148.             if (PrError() == noErr) 
  149.              {
  150.                 PrOpenPage(thePrPort, nil);
  151.                 if (PrError() == noErr) 
  152.                  {
  153.                     /********* Print from here ****************/
  154.                     
  155.                     DrawStuff ((**thePrRecHdl).prInfo.rPage);
  156.                  }
  157.              }
  158.             PrClosePage(thePrPort);
  159.         }
  160.         PrCloseDoc(thePrPort);
  161.     
  162.     if ((((TPPrint)*thePrRecHdl)->prJob.bJDocLoop == bSpoolLoop) && (PrError() == noErr))
  163.         PrPicFile(thePrRecHdl, nil, nil, nil, &theStatus);                
  164.  
  165.     PrClose();
  166.  
  167.     SetPort(oldPort);
  168.     return;
  169. }
  170.  
  171. /*______________________________________________________*/
  172. /*                 About Prog Dialog                    */
  173. /*______________________________________________________*/
  174. void showAboutMeDialog()
  175. {
  176.     GrafPtr     savePort;
  177.     DialogPtr    theDialog;
  178.     short        itemHit;
  179.  
  180.     GetPort(&savePort);
  181.     theDialog = GetNewDialog(aboutMeDLOG, nil, (WindowPtr) -1);
  182.     SetPort(theDialog);
  183.  
  184.     do {
  185.         ModalDialog(nil, &itemHit);
  186.     } while (itemHit != okButton);
  187.  
  188.     CloseDialog(theDialog);
  189.  
  190.     SetPort(savePort);
  191.     return;
  192. }
  193.  
  194. /*______________________________________________________*/
  195. /*                 Do Menu Function                     */
  196. /*______________________________________________________*/
  197. void doCommand(mResult)
  198.     long    mResult;
  199. {
  200.     int                     theMenu, theItem;
  201.     char                    daName[256];
  202.     GrafPtr                 savePort;
  203.  
  204.     theItem = LoWord(mResult);
  205.     theMenu = HiWord(mResult);
  206.     
  207.     switch (theMenu) {
  208. /*______________________________________________________*/
  209. /*                    Do Apple Menu                     */
  210. /*______________________________________________________*/
  211.         case appleID:
  212.             if (theItem == aboutMeCommand)
  213.                 showAboutMeDialog();
  214.             else {
  215.                 GetMenuItemText(mymenu0, theItem, daName);
  216.                 GetPort(&savePort);
  217.                 (void) OpenDeskAcc(daName);
  218.                 SetPort(savePort);
  219.             }
  220.             break;
  221. /*______________________________________________________*/
  222. /*                     Do File Menu                     */
  223. /*______________________________________________________*/
  224.         case fileID:
  225.             switch (theItem) {
  226.                 case startCommand:
  227.                     start();
  228.                     break;
  229.                 case printCommand:
  230.                     Print();
  231.                     break;
  232.                 case quitCommand:
  233.                     DoneFlag = TRUE;
  234.                     break;
  235.                 default:
  236.                     break;
  237.                 }
  238.             break;
  239. /*______________________________________________________*/
  240. /*                     Do Edit Menu                     */
  241. /*______________________________________________________*/
  242.         case editID:
  243.             switch (theItem) {
  244.                 default:
  245.                     break;
  246.                 }
  247.             break;
  248.         default:
  249.             break;
  250.         }
  251.     HiliteMenu(0);
  252.     return;
  253.     
  254. }
  255.  
  256.  
  257.  
  258. /*______________________________________________________*/
  259. /*               Initialization traps                   */
  260. /*______________________________________________________*/
  261. void init()
  262. {
  263.     RgnHandle        tempRgn;
  264.     
  265.     UnloadSeg(_DataInit);
  266.     InitGraf(&qd.thePort);
  267.     FlushEvents(everyEvent, 0);
  268.     InitWindows();
  269.     InitDialogs(nil);
  270.     InitCursor();
  271. /*______________________________________________________*/
  272. /*            If not right Machine then stop            */
  273. /*______________________________________________________*/
  274. /*    OSys = SysEnvirons(VERSION,&theWorld);
  275.     if(!theWorld.hasColorQD)
  276.         Color = TRUE;
  277.     else
  278.         Color = FALSE;
  279.         
  280.     if(theWorld.processor >= env68020)
  281.         CPU20 = TRUE;
  282.     else
  283.         CPU20 = FALSE;
  284.         
  285.     if(!theWorld.hasFPU)
  286.         FPU = TRUE;
  287.     else
  288.         FPU = FALSE;
  289.         
  290.     if(theWorld.systemVersion < 0x0600)
  291.         Sys6 = TRUE;
  292.     else
  293.         Sys6 = FALSE;
  294.         
  295.     if (CPU20 && (GetTrapAddress(0xA06E) != GetTrapAddress(0xA89F))) {
  296.         mySpBlockPtr = &mySpBlock;
  297.         mySpBlockPtr->spSlot = 0;
  298.         mySpBlockPtr->spTBMask = 0x03;
  299.         mySpBlockPtr->spCategory = 9;
  300.         mySpBlockPtr->spCType = 2;
  301.                 
  302.         err = SNextTypeSRsrc (mySpBlockPtr);
  303.                 
  304.         if (err) {
  305.             mySpBlockPtr = &mySpBlock;
  306.             mySpBlockPtr->spSlot = 0;
  307.             mySpBlockPtr->spTBMask = 0x03;
  308.             mySpBlockPtr->spCategory = 9;
  309.             mySpBlockPtr->spCType = F;
  310.                 
  311.             err = SNextTypeSRsrc (mySpBlockPtr);
  312.                 
  313.             if (err)
  314.                 Memboard = FALSE;
  315.             else {
  316.                 Memboard = TRUE;
  317.                 WhereBoard = mySpBlockPtr->spSlot;
  318.                 }
  319.             }
  320.         else {
  321.             Memboard = TRUE;
  322.             WhereBoard = mySpBlockPtr->spSlot;
  323.             }
  324.         }*/
  325.                     
  326. /*______________________________________________________*/
  327. /*                     Set Rects                        */
  328. /*______________________________________________________*/
  329.     screenRect = qd.screenBits.bounds;
  330.     offLeft = 0;
  331.     offTop = 0;
  332.     offRight = screenRect.right;
  333.     offBottom = screenRect.bottom;
  334.     SetRect(&BaseRect, 40, 60, 472, 282);
  335.     tempRgn = GetGrayRgn();
  336.     HLock ((Handle) tempRgn);
  337.     TotalRect = (**tempRgn).rgnBBox;
  338.     SetRect(&minRect, 80, 80, (**tempRgn).rgnBBox.right - 40, 
  339.                 (**tempRgn).rgnBBox.bottom - 40);
  340.     HUnlock ((Handle) tempRgn);
  341.  
  342. /*______________________________________________________*/
  343. /*        Open Window & set Palette & Picture           */
  344. /*______________________________________________________*/
  345.     theGDevice = GetMainDevice();
  346.     HLock ((Handle) theGDevice);
  347.     mycolors = (**(**theGDevice).gdPMap).pmTable;
  348.     numcolor = (**(**theGDevice).gdPMap).pixelSize;
  349.     HUnlock((Handle) theGDevice);
  350.     switch (numcolor) {
  351.         case 1:
  352.             numcolor = 2;
  353.             break;
  354.         case 2:
  355.             numcolor = 4;
  356.             break;
  357.         case 4:
  358.             numcolor = 16;
  359.             break;
  360.         case 8:
  361.             numcolor = 256;
  362.             break;
  363.         }
  364.     
  365.     myWindow = NewCWindow(nil, &BaseRect, "", TRUE, zoomDocProc, 
  366.                             (WindowPtr) -1, TRUE, 150);
  367.     SetPort((WindowPtr) myWindow);
  368.     DrawGrowIcon (myWindow);
  369.  
  370.     srcPalette = NewPalette (numcolor, mycolors, pmCourteous, 0);
  371.     SetPalette ((WindowPtr) myWindow, srcPalette, TRUE);
  372.     
  373. /*______________________________________________________*/
  374. /*                    Set menus                         */
  375. /*______________________________________________________*/
  376.     mymenu0 = GetMenu(appleID);
  377.     AppendResMenu(mymenu0, 'DRVR');
  378.     InsertMenu(mymenu0,0);
  379.     mymenu1 = newmenu(129,"File");
  380.     appendmenu(mymenu1,"Start;-;Print;-;Quit");
  381.     InsertMenu(mymenu1,0);
  382.     mymenu2 = newmenu(130,"Edit");
  383.     InsertMenu(mymenu2,0);
  384.     DrawMenuBar();
  385.  
  386. /*______________________________________________________*/
  387. /*                  Init variables                      */
  388. /*______________________________________________________*/
  389.     DoneFlag = FALSE;
  390.     yieldTime = 0;
  391.     return;
  392. }
  393.  
  394. main()
  395. {
  396.     Boolean        track;
  397.     long        growResult;
  398. /*______________________________________________________*/
  399. /*                   Main Event loop                    */
  400. /*______________________________________________________*/
  401.     init();
  402.     for ( ;; ) {
  403.         if (DoneFlag) {
  404.             ExitToShell();
  405.             }
  406.         if (WaitNextEvent(everyEvent, &myEvent, yieldTime, nil)) {
  407.             switch (myEvent.what) {
  408.                 case mouseDown:
  409.                     switch (FindWindow(myEvent.where, &whichWindow)) {
  410.                         case inSysWindow:
  411.                             SystemClick(&myEvent, whichWindow);
  412.                             break;
  413.                         case inMenuBar:
  414.                             doCommand(MenuSelect(myEvent.where));
  415.                             break;
  416.                         case inContent:
  417.                             break;
  418.                         case inDrag:
  419.                             DragWindow (whichWindow, myEvent.where, &TotalRect);
  420.                             EraseRect (&whichWindow->portRect);
  421.                             DrawGrowIcon (whichWindow);
  422.                             break;
  423.                         case inGrow:
  424.                             growResult = GrowWindow (whichWindow, myEvent.where,
  425.                                                     &minRect);
  426.                             SizeWindow(whichWindow, LoWord(growResult), 
  427.                                     HiWord(growResult), TRUE);
  428.                             EraseRect (&whichWindow->portRect);
  429.                             DrawGrowIcon (whichWindow);
  430.                             break;
  431.                         case inGoAway:
  432.                             track = TrackGoAway (whichWindow, myEvent.where);
  433.                             if (track)
  434.                                 CloseWindow (whichWindow);
  435.                             break;
  436.                         case inZoomIn:
  437.                             track = TrackBox (whichWindow, myEvent.where, inZoomIn);
  438.                             if (track) {
  439.                                 ZoomWindow (whichWindow, inZoomIn, TRUE);
  440.                                 EraseRect (&whichWindow->portRect);
  441.                                 DrawGrowIcon (whichWindow);
  442.                                 }
  443.                             break;
  444.                         case inZoomOut:
  445.                             track = TrackBox (whichWindow, myEvent.where, inZoomOut);
  446.                             if (track) {
  447.                                 ZoomWindow (whichWindow, inZoomOut, TRUE);
  448.                                 EraseRect (&whichWindow->portRect);
  449.                                 DrawGrowIcon (whichWindow);
  450.                                 }
  451.                             break;
  452.                         default:
  453.                             break;
  454.                         }
  455.                     break;
  456.                 case keyDown:
  457.                     break;
  458.                 case autoKey:
  459.                     break;
  460.                 case updateEvt:
  461.                     if ((WindowPtr) myEvent.message == myWindow) {
  462.                         BeginUpdate((WindowPtr) myWindow);
  463.                         EndUpdate((WindowPtr) myWindow);
  464.                         }
  465.                     break;
  466.                 case diskEvt:
  467.                     break;
  468.                 case activateEvt:
  469.                     break;
  470.                 case app4Evt:
  471.                     if ((myEvent.message << 31) == 0) { /* Suspend */
  472.                         yieldTime = 30;
  473.                         HideWindow((WindowPtr) myWindow);
  474.                         }
  475.                     else { /* Resume */
  476.                         yieldTime = 0;
  477.                         ShowWindow((WindowPtr) myWindow);
  478.                         SetPort((WindowPtr) myWindow);
  479.                         }
  480.                     break;
  481.                 default:
  482.                     break;
  483.                 }
  484.             }
  485.         }
  486. }